class Solution(object):
    def highestPeak(self, isWater):
        dist = 0
        m = len(isWater)
        n = len(isWater[0])
        now = set()
        for i in range(m):
            for j in range(n):
                if isWater[i][j] == 1:
                    isWater[i][j] = 0
                    now.add((i, j))
                else:
                    isWater[i][j] = -1
        while now:
            dist += 1
            next_set = set()
            for x, y in now:
                for ux, uy in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
                    if 0 <= ux < m and 0 <= uy < n:
                        if isWater[ux][uy] == -1:
                            isWater[ux][uy] = dist
                            next_set.add((ux, uy))
            now = next_set
        return isWater
